package com.example.magichealth;

import java.util.ArrayList;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.BCBDexther.magichealth.AppelWS;
import com.BCBDexther.magichealth.BCBSearchResult;
import com.BCBDexther.magichealth.BCBSecurity;
import com.function.magichealth.FileAndroid;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class Pharmacie extends FileAndroid {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pharmacie);
		// Show the Up button in the action bar.
		Intent intent = new  Intent();
		intent.getComponent();
		//Recherche dans la BCB d'un produit � partir d'une recherche manuelle.
		/*new BCBWS().execute();
		BCBWS MaTache=new BCBWS();
		MaTache.myQuery="test";
		MaTache.execute();
		setupActionBar();*/
		
	}
	
	public void goToMainScanBar(View view) {
		//Intent intent = new Intent(this, MainScanBar.class);
		//startActivity(intent);
		//instantiate ZXing integration class
		IntentIntegrator scanIntegrator = new IntentIntegrator(this);
		//start scanning
		scanIntegrator.initiateScan();}
	
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		//retrieve result of scanning - instantiate ZXing object
		IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
		//check we have a valid result
		if (scanningResult != null) {
			//get content from Intent Result
			String scanContent = scanningResult.getContents();
			//get format name of data scanned
			String scanFormat = scanningResult.getFormatName();
			
			ArrayList<String> scanResults = new ArrayList<String>();
			
			scanResults.add(scanContent);
			scanResults.add(scanFormat);
			super.writeFile(scanResults,"scan_results");
			
			//output to UI
			//formatTxt.setText("FORMAT: "+scanFormat);
			//contentTxt.setText("CONTENT: "+scanContent);
		}
		else{
			//invalid scan data or scan canceled
			Toast toast = Toast.makeText(getApplicationContext(), 
					"No scan data received!", Toast.LENGTH_SHORT);
			toast.show();
		}
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.pharmacie, menu);
		return true;
	}

	
private class BCBWS extends AsyncTask<Void, Void, Void> {
		
		String myQuery;
		AppelWS ws = new AppelWS();
		BCBSearchResult result = new BCBSearchResult();
	
		
		@Override
		protected Void doInBackground(Void... params) {
			String query ="";
			query = "CLAMOX";
			int type = 1;
			int baseLocation = 0;
			BCBSecurity cle=new BCBSecurity();
			result = ws.rechercheBCB(query, type, baseLocation, cle);
			return null;

		}
		@Override
		protected void onPostExecute(Void result) {
			if (result!=null)
			{
				
			}
		}
		@Override
		protected void onPreExecute() {
			
		}
	}

}
